import axios from 'axios'
import { ElMessage } from 'element-plus'

export const useRequest = () => {
  const request = axios.create({
    baseURL: '/app/admin/black-card',
    timeout: 10000,
    headers: {
      'Content-Type': 'application/json'
    }
  })

  // 请求拦截器
  request.interceptors.request.use(
    config => {
      // 在这里可以添加token等认证信息
      return config
    },
    error => {
      return Promise.reject(error)
    }
  )

  // 响应拦截器
  request.interceptors.response.use(
    response => {
      const res = response.data
      if (res.code === 0) {
        return res
      } else {
        ElMessage.error(res.msg || '请求失败')
        return Promise.reject(new Error(res.msg || '请求失败'))
      }
    },
    error => {
      ElMessage.error(error.message || '请求失败')
      return Promise.reject(error)
    }
  )

  return {
    request
  }
} 